<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="企业名称" prop="enterpriseName">
        <el-input v-model="queryParams.enterpriseName" placeholder="请输入企业名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="审核状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择申请状态">
          <el-option v-for="option in stateOptions" :key="option.value" :label="option.label"
            :value="option.value"></el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="申请人id" prop="applicantId">
        <el-input v-model="queryParams.applicantId" placeholder="请输入申请人id" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd2"
          v-hasPermi="['kjy:application:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate2"
          v-hasPermi="['system:application:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:application:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                   v-hasPermi="['system:application:export']">导出</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="applicationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="企业名称" align="center" prop="enterpriseName" />
      <el-table-column label="入库编号" align="center" prop="inStorageCode" />
      <el-table-column label="入库批次" align="center" prop="inStorageBatch" />
      <el-table-column label="高企认定文件" align="center" prop="highTechEnterpriseNotice">
        <template slot-scope="scope">
          <a v-if="scope.row.highTechEnterpriseNotice !== null" :href="getFirstItem(scope.row.highTechEnterpriseNotice)"
            target="_blank">{{ FormatFilename(getFirstItem(scope.row.highTechEnterpriseNotice)) }}</a>
        </template>
      </el-table-column>

      <el-table-column label="高企认定证书" align="center" prop="highTechEnterpriseCertificate">
        <template slot-scope="scope">
          <el-image v-if="scope.row.highTechEnterpriseCertificate !== null" style="width: 50px; height: 50px"
            :src="getFirstItem(scope.row.highTechEnterpriseCertificate)"
            :preview-src-list="[getFirstItem(scope.row.highTechEnterpriseCertificate)]"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="state">
        <template slot-scope="{ row }">
          <el-tag v-if="row.state === 0" type="primary">审核中</el-tag>
          <el-tag v-if="row.state === 1" type="success">已通过</el-tag>
          <el-tag v-if="row.state === -1" type="error">未通过</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="审核备注" align="center" prop="remark" />
      <!-- <el-table-column label="经费" align="center" prop="funding" />
      <el-table-column label="申请人id" align="center" prop="applicantId" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:application:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:application:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.current" :limit.sync="queryParams.size"
      @pagination="getList" />

    <!-- 添加或修改高企入库申请对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="提交入库申请的企业id" prop="enterpriseId">
          <el-input
            v-model="form.enterpriseId"
            placeholder="请输入提交入库申请的企业id"
          />
        </el-form-item>
        <el-form-item label="高企认定证书" prop="highTechEnterpriseCertificate">
          <el-input
            v-model="form.highTechEnterpriseCertificate"
            placeholder="请输入高企认定证书"
          />
        </el-form-item>
        <el-form-item label="高企认定文件" prop="highTechEnterpriseNotice">
          <el-input
            v-model="form.highTechEnterpriseNotice"
            placeholder="请输入高企认定文件"
          />
        </el-form-item> -->
        <el-form-item label="审核状态" prop="state">
          <el-radio v-model="form.state" :label="0">审核中</el-radio>
          <el-radio v-model="form.state" :label="1">通过</el-radio>
          <el-radio v-model="form.state" :label="-1">不通过</el-radio>
        </el-form-item>
        <el-form-item label="审核备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入审核备注" />
        </el-form-item>
        <!-- <el-form-item label="经费" prop="funding">
          <el-input v-model="form.funding" placeholder="请输入经费" />
        </el-form-item> -->
        <!-- <el-form-item label="申请人id" prop="applicantId">
          <el-input v-model="form.applicantId" placeholder="请输入申请人id" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="open2" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules2" label-width="80px">
        <el-form-item label="企业名称" prop="enterpriseName">
          <el-input v-model="searchText" placeholder="请输入企业名称" />
          <el-select v-model="selectedItem" placeholder="请选择企业" @change="handleSelectChange">
            <el-option v-for="item in filteredItems" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="入库编号" prop="inStorageCode">
          <el-input v-model="form.inStorageCode" placeholder="请输入入库编号" />
        </el-form-item>
        <el-form-item label="入库批次" prop="inStorageBatch">
          <el-input v-model="form.inStorageBatch" placeholder="请输入入库批次" />
        </el-form-item>
        <el-form-item label="高新技术企业证书" prop="highTechEnterpriseCertificate">
          <image-upload v-model="form.highTechEnterpriseCertificate" />
        </el-form-item>
        <el-form-item label="高新技术企业公示信息表" prop="highTechEnterpriseNotice">
          <file-upload v-model="form.highTechEnterpriseNotice" />
        </el-form-item>
        <!-- <el-form-item label="审核状态" prop="state">
          <el-radio v-model="form.state" :label="1">通过</el-radio>
          <el-radio v-model="form.state" :label="-1">不通过</el-radio>
        </el-form-item> -->
        <el-form-item label="审核备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入审核备注" />
        </el-form-item>
        <!-- <el-form-item label="经费" prop="funding">
          <el-input v-model="form.funding" placeholder="请输入经费" />
        </el-form-item> -->
        <!-- <el-form-item label="申请人id" prop="applicantId">
          <el-input v-model="form.applicantId" placeholder="请输入申请人id" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm2">确 定</el-button>
        <el-button @click="cancel2">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listApplication,
  getApplication,
  delApplication,
  addApplication,
  updateApplication,
  adminAddApplication,
  listEnterprise
} from "@/api/system/storage-application";
export default {
  name: "Application",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 高企入库申请表格数据
      applicationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open2: false,
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        enterpriseId: null,
        state: null,
        applicantId: null,
      },
      // 表单参数
      form: {
        id: '',
        enterpriseId: '',
        highTechEnterpriseCertificate: '',
        highTechEnterpriseNotice: '',
        state: '',
        remark: '',
        createTime: '',
        updateTime: '',
        createBy: '',
        updateBy: '',
        applicantId: '',
        enterpriseName: '',
        inStorageCode: '',
        inStorageBatch: '',
      },
      // 表单校验
      rules: {},
      rules2: {
        selectedItem: [
          { required: true, message: "企业不能为空", trigger: "blur" }
        ],
      },
      stateOptions: [
        { label: "审核中", value: 0 },
        { label: "未通过", value: -1 },
        { label: "已通过", value: 1 },
      ],
      //企业
      // 查询参数
      queryParamsEnterprise: {
        current: 1,
        size: 10,
        code: null,
        name: null,
        type: null,
        representative: null,
        businessLicense: null,
        registeredCapital: null,
        registeredAddress: null,
        telephone: null,
        mail: null,
        establishmentTime: null,
        hightechEnterprises: null,
        otherMaterials: null,
        briefIntroduction: null,
        state: null,
        highTechEnterpriseCertificate: null,
        highTechEnterpriseNotice: null,
        funding: null,
        auditState: null,
      },
      // 企业表格数据
      enterpriseList: [],
      selectedItem: null, // 选中的数据项
      searchText: null,
      isLoading: false
    };
  },
  created() {
    this.getList();

  },
  mounted() {
    this.fetchData();
  },
  computed: {
    filteredItems() {
      // 根据搜索内容进行模糊查询
      if (this.searchText != '' && this.searchText != null) {
        return this.enterpriseList.filter(item => item.name.includes(this.searchText));
      }
      else {
        return this.enterpriseList;
      }

    },
  },
  methods: {
    /** 查询高企入库申请列表 */
    getList() {
      this.loading = true;
      listApplication(this.queryParams).then((response) => {
        this.applicationList = response.data.records;
        //console.log(JSON.stringify(this.applicationList));
        this.total = response.data.total;
        this.loading = false;
      });
    },
    getFirstItem(item) {
      if (item !== null && typeof item === 'string') {
        const items = item.split(',');
        if (items.length > 0) {
          return items[0].trim();
        }
      }
      return item;
    },
    /** 查询企业列表 */
    fetchData() {
      this.loading = true;
      listEnterprise(this.queryParamsEnterprise).then((response) => {
        this.enterpriseList = response.data;
        //console.log(JSON.stringify(this.enterpriseList));
        this.loading = false;
      });
    },
    handleSelectChange() {
      if (this.selectedItem) {
        const selectedOption = this.filteredItems.find(item => item.id === this.selectedItem);
        this.searchText = selectedOption ? selectedOption.name : ''; // 根据选中的 item.id 查找对应的 item.name，并赋值给 searchText
      }
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancel2() {
      this.open2 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        enterpriseId: null,
        highTechEnterpriseCertificate: null,
        highTechEnterpriseNotice: null,
        state: null,
        remark: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        applicantId: null,
        enterpriseName: null,
        inStorageCode: null,
        inStorageBatch: null
      };
      this.selectedItem = null;
      this.searchText = null;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "审核高企入库申请";
    },
    handleAdd2() {
      this.reset();
      this.open2 = true;
      this.title = "添加高企入库申请";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getApplication(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "审核高企入库申请";
      });
    },
    handleUpdate2(row) {
      this.reset();
      const id = row.id || this.ids;
      getApplication(id).then((response) => {
        this.form = response.data;
        this.open2 = true;
        this.title = "修改高企入库申请";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateApplication(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addApplication(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    submitForm2() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.enterpriseId = this.selectedItem;
          if (this.selectedItem) {
            const selectedOption = this.filteredItems.find(item => item.id === this.selectedItem);
            this.form.enterpriseName = selectedOption ? selectedOption.name : ''; // 根据选中的 item.id 查找对应的 item.name，并赋值给 searchText
          } else {
          }
          if (this.form.id != null) {
            updateApplication(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open2 = false;
              this.getList();
            });
          } else {
            adminAddApplication(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open2 = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除选中数据项？")
        .then(function () {
          return delApplication(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
  },
};
</script>
